package com.gravata.enigma.activities;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.ListView;

import com.gravata.enigma.Confirm;
import com.gravata.enigma.R;
import com.gravata.enigma.ReloadDataListener;
import com.gravata.enigma.importer.ParseSatInfo;
import com.gravata.enigma.importer.ParseSats;
import com.gravata.enigma.importer.Sat;
import com.gravata.enigma.importer.adapter.SatAdapter;

public class ImportarSat extends ListActivity{

	private SatAdapter adapter;
	private List<Sat> sats=new ArrayList<Sat>();
	private List<Sat> satsProcessados=new ArrayList<Sat>();

	private Context context=this;
	private ActionMode mode;
	private List<Sat> selecionados;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.import_sat_screen);

		adapter=new SatAdapter(this, sats);
		setListAdapter(adapter);


		ListView listView = getListView();
 		listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

 		listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

 			private int count=0;

 		    @Override
 		    public void onItemCheckedStateChanged(ActionMode mode, int position,
 		                                          long id, boolean checked) {
 		        // Here you can do something when items are selected/de-selected,
 		        // such as update the title in the CAB
 		    	Sat sat= adapter.getItem(position);

 		    	count=checked?count+1:count-1;
 		    	mode.setTitle(count+"");
 		    	adapter.setSelection(position, checked);
 		    }


 		    @Override
 		    public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
 		        // Respond to clicks on the actions in the CAB
 		        switch (item.getItemId()) {
 		            case R.id.menu_importar:

 		            	Confirm confirm=new Confirm(context, context.getString(R.string.confirmar_importacao), context.getString(R.string.msg_importacao),
 		            			new OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog, int which) {


										 ReloadDataListener listenerSat =new ReloadDataListener() {

											@Override
											public void onReloadData() {
												adapter.clearSelection();
								            	count=0;
								                mode.finish();
												finish();
											}

											@Override
											public void onFealoadData(final List<?> list) {

												runOnUiThread(new Runnable() {
													public void run() {
													}
												});

											}
										};


				 		            	new ParseSatInfo(context, getSelecionados(), listenerSat).execute();
									}
								},
								new OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog, int which) {
										dialog.cancel();
										adapter.clearSelection();
						            	count=0;
						                mode.finish();
									}
								});
 		            	confirm.show();




 		                return true;
 		            default:
 		                return false;
 		        }
 		    }

 		    @Override
 		    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
 		        // Inflate the menu for the CAB
 		        MenuInflater inflater = mode.getMenuInflater();
 		        inflater.inflate(R.menu.sat, menu);

 		        return true;
 		    }

 		    @Override
 		    public void onDestroyActionMode(ActionMode mode) {
 		        // Here you can make any necessary updates to the activity when
 		        // the CAB is removed. By default, selected items are deselected/unchecked.
 		    	count=0;
 		    	adapter.clearSelection();
 		    }

 		    @Override
 		    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
 		        // Here you can perform updates to the CAB due to
 		        // an invalidate() request
 		        return false;
 		    }
 		});




	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		l.setItemChecked(position, true);
	}


	@Override
	protected void onResume() {
		super.onResume();
		if(sats.size()==0)
			new ParseSats(this, listener).execute();
	}


	private ReloadDataListener listener =new ReloadDataListener() {

		@Override
		public void onReloadData() {

		}

		@Override
		public void onFealoadData(final List<?> list) {

			runOnUiThread(new Runnable() {
				public void run() {
					sats=(List<Sat>) list;
					adapter.setData(sats);
				}
			});

		}
	};



	   private List<Sat> getSelecionados(){
	    	selecionados=new ArrayList<Sat>();
	    	SparseBooleanArray checked = getListView().getCheckedItemPositions();
	    	int size = checked.size(); // number of name-value pairs in the array
	    	for (int i = 0; i < size; i++) {
	    	    int key = checked.keyAt(i);
	    	    boolean value = checked.get(key);
	    	    if(value){
	    	    	selecionados.add(adapter.getItem(key));
	    	    }
	    	}
	    	return selecionados;
	    }


}
